package com.etc.service.impl;

import com.etc.dao.AreaDao;
import com.etc.entity.Area;
import com.etc.entity.FlashSale;
import com.etc.redis.FlashSaleKey;
import com.etc.redis.ProvCityAreaKey;
import com.etc.redis.RedisService;
import com.etc.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 文件名:com.etc.service.impl
 * 描述：
 * 作者:吴仕泉
 * 时间:2019/5/26 15:28
 */
@Service
public class AreaServiceImpl implements AreaService{

    @Autowired
    RedisService redisService;

    @Autowired
    AreaDao areaDao;

    //根据城市编号，查询该市的所有地区信息
    @Override
    public List<Area> findAreasByCityNum(Integer cnum) {
        //从redis缓存中查询是否有符合该条件的信息
        List<Area> areas = redisService.getList(ProvCityAreaKey.allAreaKey,String.valueOf(cnum));
        //缓存池中不存在
        if(areas == null){
            //访问数据库读取信息
            areas = areaDao.listSelectAreaByCityNum(cnum);
            if(areas == null){
                return null;
            }
            //保存在缓存池中
            redisService.setList(ProvCityAreaKey.allAreaKey,String.valueOf(cnum),areas);
        }
        return areas;
    }

    //根据地区编号查询地区的信息
    @Override
    public Area findAreaByAreaNum(Integer areaNum) {
        Area area = redisService.get(ProvCityAreaKey.oneAreaKey,String.valueOf(areaNum),Area.class);
        if(area == null){
            area = areaDao.getAreaMessageByAreaNum(areaNum.longValue());
            if(area != null){
                redisService.set(ProvCityAreaKey.oneAreaKey,String.valueOf(areaNum),area);
            }
        }
        return area;
    }
}
